package com.yonyou.pmclouds.alterandvisa.rmiimpl;


import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.alterandvisa.entity.AlterAndVisaVO;
import com.yonyou.pmclouds.alterandvisa.mapper.AlterAndVisaMapper;
import com.yonyou.pmclouds.alterandvisa.process.AlterAndVisaInsertCheckProcessor;
import com.yonyou.pmclouds.alterandvisa.process.AlterAndVisaProjectLogPostProcess;
import com.yonyou.pmclouds.alterandvisa.rmiitf.AlterAndVisaMaintain;
import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoUpdateProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.MobileTaskInsertProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.ProjectStatusCheckProcessor;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.picture.entity.PictureParamVO;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.project.processor.ProjectBillUpdateForUpdate;
import com.yonyou.pmclouds.project.processor.UpdateBillCountForDeleteProcessor;
import com.yonyou.pmclouds.project.processor.UpdateBillCountForInsertProcessor;
import com.yonyou.pmclouds.task.rmiitf.TaskRemoteService;
import com.yonyou.pmclouds.workflow.aspect.PMApprove;
import com.yonyou.pmclouds.workflow.service.rmiitf.IFlowApproveBuziService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

@Service(version = ApplicationConsts.APPLICATION_VERSION,interfaceClass= AlterAndVisaMaintain.class)
public class AlterAndVisaMaintainImpl implements AlterAndVisaMaintain,IFlowApproveBuziService{

    @Autowired
    private AlterAndVisaMapper mapper;
    @Autowired
    private PictureRemoteService pictureService;
    @Autowired
    private TaskRemoteService taskService;
    @Autowired
    private MobileTaskInsertProcessor<AlterAndVisaVO> mobileTaskInsertProcessor;
    @Autowired
    private ProjectStatusCheckProcessor<AlterAndVisaVO> projectStatusCheckProcessor;
    @Autowired
    private UpdateBillCountForInsertProcessor.UpdateBillCountForInsertProcessorFactory<AlterAndVisaVO> billCountFactory;
    @Autowired
    private ProjectBillUpdateForUpdate.ProjectBillForUpdateProcessorFactory<AlterAndVisaVO> billForUpdateProcessorFactory;
    @Autowired
    private UpdateBillCountForDeleteProcessor.UpdateBillCountForDeleteProcessorFactory<AlterAndVisaVO> billCountDeleteFactory;
    @Autowired
    private AlterAndVisaProjectLogPostProcess alterAndVisaProjectLogPostProcess;

    private static String[] UPDATE_STATUS = {AlterAndVisaVO.BILLSTATUS};
    private static final String[] UPDATE_FIELDS = {AlterAndVisaVO.PK_ALTERVISACATEGORY, AlterAndVisaVO.ESTIMATEMNY, AlterAndVisaVO.ALTERVISACONTENT, AlterAndVisaVO.APPLYDATE, AlterAndVisaVO.BILLSTATUS};

    @Override
    @PMApprove
    public AlterAndVisaVO insert(AlterAndVisaVO alterAndVisaVO) throws BusinessException {
        if(alterAndVisaVO == null){
            return null;
        }
        initInsertVO(alterAndVisaVO);   //补全字段信息
        SingleInsertBpTemplate<AlterAndVisaVO> bp = new SingleInsertBpTemplate<>(mapper);
        //更新审计信息
        bp.addBeforeProcessor(new AuditInfoInsertProcessor<AlterAndVisaVO>());
        bp.addBeforeProcessor(new AlterAndVisaInsertCheckProcessor());
        bp.addBeforeProcessor(projectStatusCheckProcessor);
        //更新单据数
        bp.addAfterProcessor(billCountFactory.getProcessor(BillTypeConst.ALTER_AND_VISA_BILLTYPE));
        //智能日志
        bp.addAfterProcessor(alterAndVisaProjectLogPostProcess);
        bp.insert(alterAndVisaVO);
        // 保存图片
        pictureService.insertPictureForPub(alterAndVisaVO, null);

        return alterAndVisaVO;
    }

    @Override
    @PMApprove(trigger = "commit")
    public AlterAndVisaVO update(AlterAndVisaVO alterAndVisaVO) throws BusinessException {
        if(alterAndVisaVO == null){
            return null;
        }
        initInsertVO(alterAndVisaVO);   //补全字段信息
        SingleUpdateBpTemplate<AlterAndVisaVO> bp = new SingleUpdateBpTemplate<AlterAndVisaVO>(mapper, UPDATE_FIELDS);
        //更新审计信息
        bp.addBeforeProcessor(new AuditInfoUpdateProcessor<AlterAndVisaVO>());
        bp.addBeforeProcessor(new AlterAndVisaInsertCheckProcessor());
        bp.addBeforeProcessor(projectStatusCheckProcessor);
        bp.addAfterProcessor(billForUpdateProcessorFactory.getProcessor(BillTypeConst.ALTER_AND_VISA_BILLTYPE));//智能日志
        bp.addAfterProcessor(alterAndVisaProjectLogPostProcess);
        bp.update(alterAndVisaVO);
        //更新单据图片
        pictureService.updateBillPicture(alterAndVisaVO, new PictureParamVO(null, null, BillTypeConst.ALTER_AND_VISA_BILLTYPE));
        return alterAndVisaVO;
    }

    @Override
    public AlterAndVisaVO approve(String pkAlterandvisa, byte billStatus) throws BusinessException {
        if(StringUtils.isEmpty(pkAlterandvisa)){
            return null;
        }
        AlterAndVisaVO alterAndVisaVO = mapper.getById(pkAlterandvisa);
        SingleUpdateBpTemplate<AlterAndVisaVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_STATUS);

        alterAndVisaVO.setBillstatus(billStatus);
        bp.addAfterProcessor(billForUpdateProcessorFactory.getProcessor(BillTypeConst.ALTER_AND_VISA_BILLTYPE));
        bp.update(alterAndVisaVO);

        return alterAndVisaVO;
    }

    @Override
    public AlterAndVisaVO reject(String pkAlterandvisa) throws BusinessException {
        if(StringUtils.isEmpty(pkAlterandvisa)){
            return null;
        }
        AlterAndVisaVO alterAndVisaVO = mapper.getById(pkAlterandvisa);
        SingleUpdateBpTemplate<AlterAndVisaVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_STATUS);
        alterAndVisaVO.setBillstatus(BillStatusConst.COMMON_REJECT_STATUS);
        bp.addAfterProcessor(billForUpdateProcessorFactory.getProcessor(BillTypeConst.ALTER_AND_VISA_BILLTYPE));
        bp.update(alterAndVisaVO);
        return alterAndVisaVO;
    }

    private void initInsertVO(AlterAndVisaVO vo){
        if(vo == null) return;
        //单据类型
        vo.setBill_type(BillTypeConst.ALTER_AND_VISA_BILLTYPE);
        //单据状态
        vo.setBillstatus(BillStatusConst.COMMON_FREE_STATUS);
        //租户
        if(StringUtils.isEmpty(vo.getPkTenant())){
            vo.setPkTenant(RuntimeEnvironment.getTenantId());
        }
    }

    @Override
    public SuperVO approveByPkBill(String pk_bill, byte billStatus) throws BusinessException{
        return approve(pk_bill, billStatus);
    }

    @Override
    public SuperVO rejectByPkBill(String pk_bill) throws BusinessException {
        return reject(pk_bill);
    }

    @Override
    public SuperVO queryDetailForWorkFlow(String pk_bill) throws BusinessException {
        AlterAndVisaVO alterAndVisaVO = mapper.getById(pk_bill);
        return alterAndVisaVO;
    }

    @Override
    public String getBillType() {
        return BillTypeConst.ALTER_AND_VISA_BILLTYPE;
    }

    @Override
    public SuperVO deleteBill(SuperVO businessVO) throws BusinessException {
        SingleDeleteBpTemplate<AlterAndVisaVO> deleteBpTemplate = new SingleDeleteBpTemplate<>(mapper);
        deleteBpTemplate.addAfterProcessor(billCountDeleteFactory.getProcessor(BillTypeConst.ALTER_AND_VISA_BILLTYPE));
        return deleteBpTemplate.delete((AlterAndVisaVO) businessVO);
    }

    @Override
    public Object getMapper() {
        return mapper;
    }
}
